package com.siss.tenancy.user.utils;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期操作工具类
 * 版权：srba
 * 修改人：王行江
 * 修改内容：新增
 * 修改时间：2016-08-19
 **/
public class ParserDate {
	
	/**
	 * 获取分
	 * @param minutes
	 * @return
	 */
	public static long getMinute( int minutes){
		long minute=60*1000*minutes;
		return minute;
	}
	
	/**
	 * 时间分差
	 * @param date
	 * @param minutes
	 * @return
	 */
	public static Date getDateCut(Date date,int minutes){
		long dateTimes=date.getTime()+getMinute(minutes);
		Date newDate=new Date(dateTimes);
		return newDate;
	}
	//格式化今天的时间格式"yyyy-MM-dd"
	public static String getToday() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
		return sdf.format(new Date());
	}
	//格式化今天的时间格式"yyyyMMddHHmmss"
	public static String getToday(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);
	}
	
	//格式化今天的时间格式"yyyyMMddHHmmss"
	public static String getTodaygetToday(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);
	}
	
	//格式化传入时间格式"yyyy-MM-dd"
	public static String getFDay(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(date);
	}
	
	//格式化传入时间格式"yyyy-MM-dd 00:00:00.000"
	public static String getUntonPayFDay(Date date) {
		String untonPayDateStr=getFDay(date)+" 00:00:00.000";
		return untonPayDateStr;
	}
	
	//格式化传入时间格式"yyyy.MM.dd"
	public static String getFDay(Date date,String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}
	
	//格式化传入时间格式"yyyy-MM-dd"
	public static String getChsFDay(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		return sdf.format(date);
	}
	
	
	//格式化传入时间格式"yyyy-MM-dd"
	public static String getChsShortFDay(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
		return sdf.format(date);
	}

	//格式化今天的时间格式"yyyy-MM-dd HH:mm:ss"
	public static String getTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
	//格式化今天的时间格式"yyyy-MM-dd HH:mm:ss"
	public static String getLongStrTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		return sdf.format(date);
	}
	
	//格式化今天的时间格式"yyyy-MM-dd HH:mm:ss"
	public static String getNowTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}
	//格式化入的时间格式"yyyy-MM-dd HH:mm:ss"
	public static String dateToString(Date time) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		String ctime = formatter.format(time);
		return ctime;
	}

	//将传入的时间格试字符串转换成时间类型 
	public static Date parseDate(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	//将传入的时间格试字符串转换成时间类型 
	public static Date parseYYYYMMDDHHMMSSDate(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date=null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	//转换时间类型 1124210219
	public static Date parseMMddHHmmss(String str){
		SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
		Date date=null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	//转换时间类型 1124
	public static Date parseYYYYMMdd(String str){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date=null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	//传入日期串取整，适用于银联查询
	public static String paseDateStrToStr(String dateStr){
		return getFDay(parseYYYYMMDDHHMMSSDate(dateStr))+" 00:00:00.000";
	}
	
	//将传入的时间格试字符串转换成时间类型 
	public static Date parseDateByShortDateStr(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date=null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	//计算并返回上一工作日的时间格式"yyyy-MM-dd HH:mm:ss"
	@SuppressWarnings("deprecation")
	public static String getStepWorkDay(){
		Date today=new Date();
		String dateTime=getDay(today)+" 00:00:00";
		today=parseDate(dateTime);
		int weekNo=today.getDay();
		today=1==weekNo?new Date(today.getTime()-3*24 * 60 * 60 * 1000):0==weekNo?new Date(today.getTime()-2*24 * 60 * 60 * 1000):new Date(today.getTime()-24 * 60 * 60 * 1000);
		return dateToString(today);
	}
	
	//计算传入时间并返回上一工作日的时间格式"yyyy-MM-dd HH:mm:ss"
	@SuppressWarnings("deprecation")
	public static String getStepWorkDay(String dateStr){
		Date today=parseDate(dateStr);
		String dateTime=getDay(today)+" 00:00:00";
		today=parseDate(dateTime);
		int weekNo=today.getDay();
		today=1==weekNo?new Date(today.getTime()-3*24 * 60 * 60 * 1000):0==weekNo?new Date(today.getTime()-2*24 * 60 * 60 * 1000):new Date(today.getTime()-24 * 60 * 60 * 1000);
		return dateToString(today);
	}
	@SuppressWarnings("deprecation")
	public static String getWeek(String dateStr){
		Date day=parseDate(dateStr);
		//返加星期几，0为星期日
		int weekNo=day.getDay();
		String numStr=1==weekNo?"一":2==weekNo?"二":3==weekNo?"三":4==weekNo?"四":5==weekNo?"五":6==weekNo?"六":"日";
		return "星期"+numStr;
	}
	
	/**
	 * 获取指定日期年份
	 * @param date
	 * @return
	 */
	public static int getYear(Date date){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(date);
	     int year = cal.get(Calendar.YEAR);//获取年份
	     return year;
	}
	
	/**
	 * 获取指定日期月份
	 * @param date
	 * @return
	 */
	public static int getMonth(Date date){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(date);
	     int month = cal.get(Calendar.MONTH);//获取年份
	     return month;
	}
	
	/**
	 * 获取指定日期年份
	 * @param date
	 * @return
	 */
	public static int getDay(Date date){
		 Calendar cal = Calendar.getInstance();
		 cal.setTime(date);
	     int day = cal.get(Calendar.DAY_OF_MONTH);//获取年份
	     return day;
	}
	
	/**
	 * 根据日期加减天数
	 * @param date
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date addOrCutDay(Date date,int day){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(calendar.DAY_OF_YEAR, day);
		date=calendar.getTime();
	     return date;
	}
	
	
	/**
	 * 根据日期加减月数
	 * @param date
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date addOrCutMonth(Date date,int month){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(calendar.MONTH, month);
		date=calendar.getTime();
	     return date;
	}
	
	/**
	 * 根据日期加减年数
	 * @param date
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date addOrCutYear(Date date,int year){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(calendar.YEAR, year);
		date=calendar.getTime();
	     return date;
	}
	
	/**  
     * 计算两个日期之间相差的天数  
     * @param smdate 较小的时间 
     * @param bdate  较大的时间 
     * @return 相差天数 
     * @throws ParseException  
     */    
    public static int daysBetween(Date smdate,Date bdate) 
    {    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        try {
			smdate=sdf.parse(sdf.format(smdate));
			 bdate=sdf.parse(sdf.format(bdate));
		} catch (ParseException e) {
			e.printStackTrace();
		}  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(smdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(bdate);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));           
    }    
      
	/** 
	*字符串的日期格式的计算 
	*/  
    public static int daysBetween(String smdate,String bdate) {  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar cal = Calendar.getInstance();    
        long time1=0L;
		try {
			cal.setTime(sdf.parse(smdate));    
			time1 = cal.getTimeInMillis();                 
			cal.setTime(sdf.parse(bdate));
		} catch (ParseException e) {
			e.printStackTrace();
		}    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));     
    }  
	
	/**
	 * 日期比较
	 * date1>date2=1 date1=date2=0 date1<date2-1
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException 
	 */
	public static int compareToDate(Date date1,Date date2){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
		try {
			date1=sdf.parse(sdf.format(date1));  
			date2=sdf.parse(sdf.format(date2));
		} catch (Exception e) {
			e.printStackTrace();
		}  
		return date1.compareTo(date2);
	}
	
	/**
	 * 传入日期获取当前日期的凌晨时间
	 * @param date
	 * @return
	 */
	@SuppressWarnings("static-access")
	public static Date getDayBreakTime(Date date){
		//将日期转成calendar
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(calendar.HOUR, 0);
		calendar.set(calendar.MINUTE, 0);
		calendar.set(calendar.SECOND, 0);
		date = calendar.getTime();
		return date;
	}

	/**
	 * 获取当前月份的第一天或者最后一天
	 * @param now
	 * @param min
	 * @param brfore_now_after 012 获取上月，当前，下个月 的月初和月末
	 * @return
	 */
	public static Date getMinOrMaxDayOfMonth(Date now,boolean min,int brfore_now_after){
		Date dayOfMonth = null;
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		//获取当前月份
		Integer month = calendar.get(Calendar.MONTH);
		if(brfore_now_after == 0){
			month--;
		}
		if(brfore_now_after == 2){
			month++;
		}
		calendar.set(Calendar.MONTH,month);
		if(min){
			calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
			dayOfMonth = calendar.getTime();
		}else{
			calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
			dayOfMonth = calendar.getTime();
		}
		return dayOfMonth;
	}

	public static Date getMinOrMaxDayOfWeek(Date now,boolean min){
		Date dayOfWeekD = null;
		int aWeek = 7;
		int dayOfWeek = getDayOfWeek(now);//获取当前是周几
		int decrementDay = dayOfWeek-1;
		int addDay = aWeek-dayOfWeek;
		if(min){
			dayOfWeekD = addOrCutDay(now,-decrementDay);
		}else{
			dayOfWeekD = addOrCutDay(now,addDay);
		}
		return dayOfWeekD;
	}

	/**
	 *
	 * 功能描述：返回两个日期之间的天数，date2是比较大的日期
	 * 作   者： liyp
	 * 创建时间： 2018/12/4 17:03
	 * 参数: [date1, date2]
	 *  返回值： int
	 */
	public static int differentDays(Date date1,Date date2)
	{
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int day1= cal1.get(Calendar.DAY_OF_YEAR);
		int day2 = cal2.get(Calendar.DAY_OF_YEAR);

		int year1 = cal1.get(Calendar.YEAR);
		int year2 = cal2.get(Calendar.YEAR);
		if(year1 != year2)   //同一年
		{
			int timeDistance = 0 ;
			for(int i = year1 ; i < year2 ; i ++)
			{
				if(i%4==0 && i%100!=0 || i%400==0)    //闰年
				{
					timeDistance += 366;
				}
				else    //不是闰年
				{
					timeDistance += 365;
				}
			}

			return timeDistance + (day2-day1) ;
		}
		else    //不同年
		{
			return day2-day1;
		}
	}

	/**
	 * 周日排在第一位，所以要减去1
	 * @param now
	 * @return
	 */
	public static int getDayOfWeek(Date now){
//		now = addOrCutDay(now,-7);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		return calendar.get(Calendar.DAY_OF_WEEK)-1;
	}

	/// 获取日期12个月
	public static String[] get12Months(Date date) {

		String[] last12Months = new String[12];
		Calendar cal = Calendar.getInstance();
		cal.setTime (date);
		for (int i = 0; i < 12; i++) {
			last12Months[11 - i] = getFDay (cal.getTime (),"yyyy-MM");
			cal.add (Calendar.MONTH,  - 1); // 逐次往前推1个月
		}
		return last12Months;
	}

	public static String[] get10Weeks(Date date) {

		String[] last10Weeks = new String[10];
		Calendar cal = Calendar.getInstance();
		cal.setTime (date);
		for (int i = 0; i < 10; i++) {
			last10Weeks[9 - i] = getFDay (cal.getTime (),"yyyy-ww");
			cal.add (Calendar.DAY_OF_MONTH,  - 7); // 逐次往前推1周
		}
		return last10Weeks;
	}

	public static String[] get7Days(Date date) {

		String[] last7Days = new String[7];
		Calendar cal = Calendar.getInstance();
		cal.setTime (date);
		for (int i = 0; i < 7; i++) {
			last7Days[6 - i] = getFDay (cal.getTime (),"yyyy-MM-dd");
			cal.add (Calendar.DAY_OF_YEAR,  - 1); // 逐次往前推1天
		}
		return last7Days;
	}

	public static Date getWeekDayMonday(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setFirstDayOfWeek (Calendar.MONDAY);
		calendar.setTime(date);
		int dayWeek = calendar.get (Calendar.DAY_OF_WEEK);
		if(dayWeek == 1){
			dayWeek = 8;
		}
		calendar.add (Calendar.DATE,calendar.getFirstDayOfWeek () - dayWeek);

		return calendar.getTime ();
	}

	public static void main(String[] args) throws UnknownHostException, ParseException{
		int a = differentDays(new SimpleDateFormat("yyyy-MM-dd").parse("2018-12-19"),new Date());
		System.out.println(a);

	}
}
